package com.wsoft.oss.utils;

import com.wsoft.utils.FileUtil;
import io.minio.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * Minio操作
 *
 * @author: juiet
 * @date: 2024年09月24日 11:30
 */
public class MinioUtil {
    private static MinioClient minioClient = null;
    /**
     * 上传文件
     * @param file
     * @return
     */
    public static String upload(String minioUrl,String minioName,String minioPass,String bucketName,MultipartFile file) {
        String fileUrl = "";
        try {
            initMinio(minioUrl, minioName,minioPass);

            InputStream stream = file.getInputStream();
            // 获取文件名
            String orgName = file.getOriginalFilename();
            if("".equals(orgName)){
                orgName=file.getName();
            }
            orgName = FileUtil.getFileNameNoEx(orgName);
            String objectName = "/"
                    +( orgName.indexOf(".")==-1
                    ?orgName + "_" + System.currentTimeMillis()
                    :orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."))
            );


            PutObjectArgs objectArgs = PutObjectArgs.builder().object(objectName)
                    .bucket(bucketName)
                    .contentType("application/octet-stream")
                    .stream(stream,stream.available(),-1).build();
            minioClient.putObject(objectArgs);
            stream.close();
            fileUrl = minioUrl+bucketName+"/"+objectName;
        }catch (Exception e){
            e.printStackTrace();
        }
        return fileUrl;
    }

    /**
     * 初始化客户端
     * @param minioUrl
     * @param minioName
     * @param minioPass
     * @return
     */
    private static MinioClient initMinio(String minioUrl, String minioName, String minioPass) {
        if (minioClient == null) {
            try {
                minioClient = MinioClient.builder()
                        .endpoint(minioUrl)
                        .credentials(minioName, minioPass)
                        .build();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return minioClient;
    }

    public static void main(String[] args) {
        String minioUrl = "http://121.224.76.243:9000";
        String accessKey = "3meNwaQyzqS6XJCYKkxB";
        String secretKey = "3s7eaCzR3CWqCK5UBnIZ1srUspQ9bQn0TtSCAapF";
        String bucketName = "wsoft001";
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(minioUrl)
                            .credentials(accessKey, secretKey)
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object("asiaphotos-2015.zip")
                            .filename("C:\\Users\\wangj\\Desktop\\velocity.log")
                            .build());
            System.out.println(
                    "'/home/user/Photos/asiaphotos.zip' is successfully uploaded as "
                            + "object 'asiaphotos-2015.zip' to bucket 'asiatrip'.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
